package com.nuanshui.heatedloan.credit.pay.utils;

import com.aipg.common.AipgRsp;
import com.allinpay.XmlTools;
import com.nuanshui.framework.lang.DateUtil;
import com.nuanshui.framework.springContext.Environment;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.credit.pay.*;
import com.nuanshui.heatedloan.credit.utils.Sequence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.List;

public class AllinPayUtil {

    private static Logger logger = LoggerFactory.getLogger(AllinPayUtil.class);

    //正式商户号
    public   static String merchantId = "200290000018288";
    //正式证私钥密码
    public   static String pfxPassword = "111111";
    //正式用户密码
    public   static String password = "111111";
    //正式证书
    public   static String pfxPath= "config/20029000001828804.p12";
    //正式用户名
    public   static String userName = "20029000001828804";
    //正式证书
    public  static  String tltcerPath= "config/allinpay-pds.cer";

    public static String getPath(String targetPath)
    {
        String path="";
        if(Environment.isAliyunEnv())
        {
            path="/home/root/"+targetPath;
            try {
                path= URLDecoder.decode(path,"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        else
        {
            try {
                path=URLDecoder.decode(AllinPayUtil.class.getClassLoader().getResource(targetPath).toURI().getPath(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    /**
     * 报文签名
     * @param xml
     * @return
     * @throws Exception
     */
    public static String signMsg(String xml) throws Exception{

        String path =getPath(pfxPath);

        LogUtil.info(logger,"通联请求","签名路径:"+path);
        xml=XmlTools.signMsg(xml,path,pfxPassword, false);

        return xml;
    }

    /**
     * 验证签名
     * @param msg
     * @return
     * @throws Exception
     */
    public static boolean verifyMsg(String msg,boolean isFront) throws Exception{

        String path =getPath(tltcerPath);

        boolean flag= XmlTools.verifySign(msg, path, false,isFront);
        System.out.println("验签结果["+flag+"]") ;
        return flag;
    }

    public  static void logOrderPay(Logger logger,PayRequest request, String msg)
    {
        if(TransRequestCode.isPayCode(request.getTransRequestCode()))
        {
            LogUtil.info(logger,"支付流程  订单号:"+request.getOrderNo()+";交易代码:"+request.getTransRequestCode(), msg);
        }
    }

     public static <T> List<T> getTrxData(AipgRsp aipgrsp)
     {
         List<T> list=(List<T>)aipgrsp.getTrxData();
         return list;
     }

    public static <T> T getTrxDataItem(AipgRsp aipgrsp)
    {
        T item=null;


        if(aipgrsp.getINFO().getTRX_CODE().equals(TransRequestCode.TRX_CODE_BANKCARD_QUERY))
        {
            item=(T)aipgrsp.getQCARDBINRSP();

            return item;
        }

        List<T> list=(List<T>)aipgrsp.getTrxData();

        if(!CollectionUtils.isEmpty(list))
        {
            item=list.get(0);
        }
        return item;
    }

    /**
     * 创建批次流水号:商户号+时间日期+6位随机数
     * @param merchantId
     * @return
     */
    public  static  String getReqSn(String merchantId){
        return merchantId+"-"+ DateUtil.getToday("yyyyMMddHHmmss")+ Sequence.getGenerationRandom(6);
    }
}
